package jp.co.jbuy.common;

import java.util.HashMap;

import jp.co.jbuy.db.query.SystemMessageQuery;

public class MessageManager {
	private static HashMap<String,String> zhCnMap=new HashMap<String,String>();
	private static HashMap<String,String> zhTwMap=new HashMap<String,String>();
	private static HashMap<String,String> jpMap=new HashMap<String,String>();
	private static HashMap<String,String> enMap=new HashMap<String,String>();
	
	public static void loadAll(
			java.sql.Connection conn) 
			throws java.sql.SQLException {
		SystemMessageQuery query = new SystemMessageQuery();
		try {
			query.searchAll(conn);
			while(query.next()) {
				String key = query.getPageId()+"_"+query.getMessageKey();
				zhCnMap.put(key,query.getContentZhCn());
				zhTwMap.put(key,query.getContentZhTw());
				jpMap.put(key,query.getContentJp());
				enMap.put(key,query.getContentEn());
			}
		} finally {
			query.close();
		}
	}
	public static String getMessage(String language,String key,String defaultMessage){
		String message="";
		if(language.equalsIgnoreCase("zh_cn")){
			message=zhCnMap.get(key);
		}else if(language.equalsIgnoreCase("zh_tw")){
			message=zhTwMap.get(key);
		}else if(language.equalsIgnoreCase("jp")){
			message=jpMap.get(key);
		}else if(language.equalsIgnoreCase("en")){
			message=enMap.get(key);
		}
		if(message==null || message.equalsIgnoreCase("")){
			message=defaultMessage;
		}
		return message;
	}
}
